package com.cskaoyan.service.wx_collect_service;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.wx_bo.AddOrDeleteWxCollectBO;
import com.cskaoyan.bean.wx_vo.collect.BaseCollectionVO;
import com.cskaoyan.bean.wx_vo.collect.ListWxCollectionVO;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.until.CurrentUserUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/9 17:27
 */
@Service
public class WxCollectServiceImpl implements WxCollectService{
    @Autowired
    CollectMapper collectMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public void addOrDelete(AddOrDeleteWxCollectBO addOrDeleteWxCollectBO) {
        Byte type = addOrDeleteWxCollectBO.getType();
        Integer valueId = addOrDeleteWxCollectBO.getValueId();
        Integer id = CurrentUserUtil.getId();
        // 先看该数据是否存在
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andTypeEqualTo(type).andValueIdEqualTo(valueId).andUserIdEqualTo(id);
        int isExist = this.collectMapper.selectByExample(collectExample).size();
        if (isExist == 0) {         // 插入数据
            // 构造POJO
           Collect collect = Collect.builder().type(type).valueId(valueId).userId(id)
                    .addTime(new Date()).updateTime(new Date()).build();
            this.collectMapper.insertSelective(collect);
        } else {
            this.collectMapper.deleteByExample(collectExample); // 删除数据
        }
    }

    @Override
    public ListWxCollectionVO list(Byte type, Integer page, Integer limit) {
        // 开启分页
        PageHelper.startPage(page, limit);
        // 拿到当前用户id
        Integer id = CurrentUserUtil.getId();
        // 查询当前用户收藏的商品 type=0
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andUserIdEqualTo(id).andTypeEqualTo(type);
        List<Collect> collects = this.collectMapper.selectByExample(collectExample);
        // 获取分页信息
        PageInfo<Collect> pageInfo = new PageInfo<>();
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        // POJO转VO
        // 根据valueID查询对应的商品信息
        ArrayList<BaseCollectionVO> baseCollectionVOS = new ArrayList<>();
        for (Collect collect : collects) {
            Goods goods = this.goodsMapper.selectByPrimaryKey(collect.getValueId());
            BaseCollectionVO baseCollectionVO = BaseCollectionVO.builder().brief(goods.getBrief())
                    .picUrl(goods.getPicUrl()).valueId(collect.getValueId())
                    .name(goods.getName()).id(collect.getId()).type(0)
                    .retailPrice(goods.getRetailPrice()).build();
            baseCollectionVOS.add(baseCollectionVO);
        }
        // 最终结果的封装
/*        ListWxCollectionVO listWxCollectionVO = ListWxCollectionVO.builder().list(baseCollectionVOS).build();
        listWxCollectionVO.setLimit(limit);
        listWxCollectionVO.setPage(page);
        listWxCollectionVO.setPages(pages);
        listWxCollectionVO.setTotal(total);*/
        ListWxCollectionVO listWxCollectionVO = ListWxCollectionVO.builder().list(baseCollectionVOS)
                .limit(limit).page(page).pages(pages).total(total).build();
        return listWxCollectionVO;
    }
}
